package cn.edu.ujn.liuklab3.service;

import cn.edu.ujn.liuklab3.entity.Restaurant;
import cn.edu.ujn.liuklab3.dao.RestaurantDao;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

@Service("restaurantService")
public class RestaurantServiceImpl implements RestaurantService {
    @Resource
    private RestaurantDao restaurantDao;

    @Override
    public cn.edu.ujn.util.Page<Restaurant> selectAll(Restaurant restaurant) {
        List<Restaurant> restaurants = restaurantDao.selectAll(restaurant);
        cn.edu.ujn.util.Page<Restaurant> page = new cn.edu.ujn.util.Page<>();
        page.setPage(restaurant.getStart());
        page.setRows(restaurants);
        page.setSize(restaurant.getRows());
        page.setTotal(restaurantDao.selectCount());
        return page;
    }
    @Override
    public Restaurant queryById(Integer restId) {
        return this.restaurantDao.queryById(restId);
    }

    @Override
    public Page<Restaurant> queryByPage(Restaurant restaurant, PageRequest pageRequest) {
        long total = this.restaurantDao.count(restaurant);
        return new PageImpl<>(this.restaurantDao.queryAllByLimit(restaurant, pageRequest), pageRequest, total);
    }

    @Override
    public Restaurant insert(Restaurant restaurant) {
        this.restaurantDao.insert(restaurant);
        return restaurant;
    }

    @Override
    public Restaurant update(Restaurant restaurant) {
        this.restaurantDao.update(restaurant);
        return this.queryById(restaurant.getRestId());
    }

    @Override
    public boolean deleteById(Integer restId) {
        return this.restaurantDao.deleteById(restId) > 0;
    }

    @Override
    public List<Restaurant> findAll() {
        return restaurantDao.findAll();
    }
}
